package com.schoolMall.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author:zyh
 * @Time:2021-08-29-16:30
 * @email:1269231889@qq.com
 */
public class HttpServletRequestUtil {

    public static long getLong(HttpServletRequest request, String key) {
        try {
            return Long.decode(request.getParameter(key));
        } catch (NumberFormatException e) {
            return -1L;
        }
    }

    public static int getInt(HttpServletRequest request, String key) {
        try {
            return Integer.decode(request.getParameter(key));
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    public static Boolean getBolean(HttpServletRequest request, String key) {
        try {
            return Boolean.valueOf(request.getParameter(key));
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static double getDouble(HttpServletRequest request, String key) {
        try {
            return Double.valueOf(request.getParameter(key));
        } catch (NumberFormatException e) {
            return -1d;
        }
    }

    public static String getString(HttpServletRequest request,String key){
        try{
            String parameter = request.getParameter(key);
            if(Objects.nonNull(parameter)){
                parameter = parameter.trim();
            }
            if("".equals(parameter)){
                parameter = null;
            }
            return parameter;
        }catch (Exception e){
            return null;
        }
    }
}
